package com.study.a12FinnalTest;

import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Cilent {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1", 9000);
        System.out.println("连接服务器成功成功");
        System.out.println("欢迎来到聊天室");
        System.out.println("1登录");
        System.out.println("2注册");
        System.out.println("请输入您的选择");

        Scanner sc = new Scanner(System.in);
        int choose = sc.nextInt();
        // 解决 Scanner 缓冲区残留问题：读取并丢弃换行符
        sc.nextLine();
        OutputStream os = socket.getOutputStream();
        os.write((choose + "\n").getBytes());
        os.flush();
        if (choose == 1) {
            int count = 0;


            // 用循环确保用户名和密码依次输入（修复问题1）
            while (count < 2) {
                if (count == 0) {
                    System.out.println("登录");
                    System.out.println("请输入您的用户名");
                    String name = sc.nextLine();  // 此时能正确等待用户输入
                    os.write((name + "\n").getBytes());
                    os.flush();
                    count++;  // count 变为 1，下次循环进入密码输入
                } else if (count == 1) {
                    System.out.println("请输入您的密码");
                    String password = sc.nextLine();
                    os.write((password + "\n").getBytes());
                    os.flush();
                    count++;  // count 变为 2，退出循环
                }

            }

            System.out.println("登录成功，开始聊天（输入 exit 退出）");
            new Thread(() -> {
                try {
                    InputStreamReader isr = new InputStreamReader(socket.getInputStream());
                    int b;
                    // 持续读取服务器发送的字节流
                    while ((b = isr.read()) != -1) {
                        System.out.print((char) b); // 打印接收到的消息
                    }
                    System.out.println("\n服务器已断开连接");
                } catch (IOException e) {
                    System.out.println("\n连接已关闭");
                }
            }).start();
            while (true) {
                String str = sc.nextLine();
                os.write((str + "\n").getBytes());
                os.flush();
                if (str.equals("exit")) {
                    break;
                }
            }


           /* // 接受服务器回写数据
            InputStreamReader isr = new InputStreamReader(socket.getInputStream());
            int b;
            while ((b = isr.read()) != -1) {
                System.out.print((char) b);
            }*/
        }else if(choose == 2){
            int count = 0;

            // 用循环确保用户名和密码依次输入（修复问题1）
            while (count < 2) {
                if (count == 0) {
                    System.out.println("注册");
                    System.out.println("请输入您的用户名");
                    String name = sc.nextLine();  // 此时能正确等待用户输入
                    os.write((name + "\n").getBytes());
                    os.flush();
                    count++;  // count 变为 1，下次循环进入密码输入
                } else if (count == 1) {
                    System.out.println("请输入您的密码");
                    String password = sc.nextLine();
                    os.write((password + "\n").getBytes());
                    os.flush();
                    count++;  // count 变为 2，退出循环
                    System.out.println("注册成功");
                }
        }


    }sc.close();
        socket.close();

}}

